/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.CustomerEntity;
import entity.PersonEntity;
import entity.SalesLeadEntity;
import entity.SalesQuotationEntity;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author edd
 */
@Stateful
@LocalBean
public class SalesQuotationSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    SalesQuotationEntity salesQuotation;
    PersonEntity person;
    SalesLeadEntity salesLead;
    CustomerEntity customer;
   /* 
    public SalesQuotationSessionBean() {
        
    }
    
    public void addSalesQuotation(Long personId, HashMap<Long,Integer> productQuantity, 
            double price) throws Exception {
        
        person = em.find(PersonEntity.class, personId);
        if(person == null)
            throw new Exception("Sales Lead or Customer does not exist.");
        else {
            //method to calculate indicative lead time to be inserted here
            //dummy indicative lead time value
            //salesQuotation = new SalesQuotationEntity(productQuantity, price, 100);
            customer = em.find(CustomerEntity.class, personId);
            if(customer != null) {
                salesQuotation.setCustomer(customer);
            }
            else {
                salesLead = em.find(SalesLeadEntity.class, personId);
                salesQuotation.setSalesLead(salesLead);
            }
            em.persist(salesQuotation);
        }
    }
    
    public void editSalesQuotation(Long salesQuotationId, HashMap<Long, Integer> productQuantity, 
            double price, int indicativeLeadTime) throws Exception {
        
        salesQuotation = em.find(SalesQuotationEntity.class, salesQuotationId);
        if(salesQuotation == null) 
            throw new Exception("Sales Quotation does not exist.");
        else {
            //salesQuotation.setProductQuantity(productQuantity); //allow to edit?
            //salesQuotation.setPrice(price); 
            salesQuotation.setIndicativeLeadTime(indicativeLeadTime); //override calculated lead time
            em.persist(salesQuotation);
        }
    }
    
    public void deleteSalesQuotation(Long salesQuotationId) throws Exception {
        
        salesQuotation = em.find(SalesQuotationEntity.class, salesQuotationId);
        if(salesQuotation == null)
            throw new Exception("Sales Quotation does not exist");
        else if (salesQuotation.isArchived()) 
            throw new Exception("Sales Quotation has already been deleted.");
        else {
            salesQuotation.setArchived(true);
            em.persist(salesQuotation);
        }
    }
    
    public ArrayList<SalesQuotationEntity> viewSalesQuotation() {
        
        Query query = em.createQuery("SELECT s FROM SalesQuotation s WHERE s.archived = :notArchived "
                + "ORDER BY s.id desc");
        query.setParameter("notArchived", "false");
        
        ArrayList listSalesQuotation = new ArrayList<SalesQuotationEntity>();
        
        for(Object o : query.getResultList()) {
            SalesQuotationEntity s = (SalesQuotationEntity) o;
            listSalesQuotation.add(o);
        }
        return listSalesQuotation;
    }
    */
}
